package com.ssm.Dao;

import com.ssm.Utils.DBCPUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CateDao {

    //查询所有商品的信息
    public List<Object> find() {

        String sql="select * from category";
        Object[] objects = null;
        ResultSet resultSet = DBCPUtils.executeQuery(sql);
        List<Object> objectList = new ArrayList<>();

        try {
            while(resultSet.next()){
                String c_name=resultSet.getString("c_name");
                String c_place=resultSet.getString("c_place");
                String c_count=resultSet.getString("c_count");
                String c_picture=resultSet.getString("c_picture");

                objects= new Object[]{c_name, c_count,c_place, c_picture};
                objectList.add(objects);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBCPUtils.closeAll(DBCPUtils.rs,DBCPUtils.pstmt,DBCPUtils.connection);
        }
        return objectList;
    }

    public int findByPicture(String picture) {

        String sql="select c_id from category where c_picture=?";

        Object[] objects={picture};

        ResultSet resultSet = DBCPUtils.executeQuery(sql, objects);

        int cateId = 0;
        try {
            while (resultSet.next()){
                cateId=resultSet.getInt("c_id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //关闭数据库的方法
            DBCPUtils.closeAll(resultSet, DBCPUtils.pstmt, DBCPUtils.connection);
        }

        return cateId;
    }
}
